package com.cskaoyan.service;

import com.cskaoyan.bean.pojo.Collect;
import com.cskaoyan.bean.pojo.CollectExample;
import com.cskaoyan.bean.pojo.Goods;
import com.cskaoyan.bean.pojo.GoodsExample;
import com.cskaoyan.bean.vo.CollectAddordeleteVO;
import com.cskaoyan.bean.vo.wx.CollectListBean;
import com.cskaoyan.bean.vo.wx.CollectListVO;
import com.cskaoyan.dao.CollectMapper;
import com.cskaoyan.dao.GoodsMapper;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@SuppressWarnings("all")
@Service
public class CollectServiceImpl implements CollectService {
    @Autowired
    CollectMapper collectMapper;
    @Autowired
    GoodsMapper goodsMapper;

    @Override
    public CollectListVO collectList(Integer page, Integer size, Integer userId) {
        PageHelper.startPage(page, size);
        CollectExample collectExample = new CollectExample();
        CollectExample.Criteria criteria = collectExample.createCriteria();
        criteria.andTypeEqualTo((byte) 0).andDeletedEqualTo(false).andUserIdEqualTo(userId);
        List<Collect> collects = collectMapper.selectByExample(collectExample);

        List<CollectListBean> collectList = new ArrayList<>();
        for (Collect collect : collects) {
            GoodsExample goodsExample = new GoodsExample();
            Goods goods = goodsMapper.selectByPrimaryKey(collect.getValueId());
            collectList.add(new CollectListBean(goods.getBrief(), goods.getPicUrl(),
                    collect.getValueId(), goods.getName(), collect.getId(), collect.getType(), goods.getRetailPrice().doubleValue()));
        }
        int count = (int) collectMapper.countByExample(collectExample);
        return new CollectListVO(count, collectList);
    }

    @Override
    public CollectAddordeleteVO collectAddordelete(int type, int valueId, int id) {
        String collect;
        CollectExample collectExample = new CollectExample();
        CollectExample.Criteria criteria = collectExample.createCriteria();
        criteria.andUserIdEqualTo(id).andTypeEqualTo((byte) type).andValueIdEqualTo(valueId);
        List<Collect> collects = collectMapper.selectByExample(collectExample);
        if (collects.size() == 0) {
            collect = "add";
            collectMapper.insertSelective(new Collect(null, id, valueId, (byte) type, new Date(), new Date(), false));
        } else {
            if (collects.get(0).getDeleted()) {
                collect = "add";
                Collect collect1 = new Collect();
                collect1.setId(collects.get(0).getId());
                collect1.setDeleted(false);
                collectMapper.updateByPrimaryKeySelective(collect1);
            } else {
                collect = "delete";
                Collect collect1 = new Collect();
                collect1.setId(collects.get(0).getId());
                collect1.setDeleted(true);
                collectMapper.updateByPrimaryKeySelective(collect1);
            }
        }
        return new CollectAddordeleteVO(collect);
    }


}
